<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<table id="tblSendEmail" width="100%" align="center" border="0"  class="list" cellpadding="5">
	<tr>
		<td align="right" width="150">Send to students</td>
		<td align="left">
			<select name="to" id="to" onchange="changeDisplaySendEmail(this)">
				<option value="1">Online</option>
				<option value="2">Offline</option>
				<option value="3">University</option>
				<option value="4">Absent in number of days</option>
				<option value="5">Active</option>
				<option value="6">Inactive</option>
				<option value="7">All</option>
			</select>
			<select id="university" name="university" style="display:none">
			<?php				
				$query = mysql_query("SELECT * FROM university_info");
				while($q = mysql_fetch_array($query))
				{ 
					echo "<option value='" . $q['university_name'] . "'>" . $q['university_name'] . "</option>";
				}
			?>
			</select>
			<input id="txtNumDaysAbsent" name="txtNumDaysAbsent" type="textbox" value="30" style="width:60px;display:none"/>
		</td>
	</tr>
	<tr>
		<td align="right">Subject</td>
		<td><input name="subject" id="subject" type="text" style="width:100%"/></td>
	</tr>
	<tr>
		<td colspan="2">
			<textarea name="content" id="content" class="textarea" cols="45" rows="8"></textarea>
			<script type="text/javascript">var editor = CKEDITOR.replace( 'content',{skin : 'kama'});</script>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="right">
			<input type="button" value="Send" style="width:120px" onclick="sendEmail()"/>&nbsp;&nbsp;<input type="button" value="Clear" style="width:120px" onclick="clearContentEmail()"/>
		</td>
	</tr>
</table>
<script type="text/javascript">	
	function changeDisplaySendEmail(item)
	{
		if (item.selectedIndex == 2)
		{
			$("#university").show();
			$("#txtNumDaysAbsent").hide();
		}
		else if (item.selectedIndex == 3)
		{
			$("#university").hide();
			$("#txtNumDaysAbsent").show();
		}
		else
		{
			$("#university").hide();
			$("#txtNumDaysAbsent").hide();
		}
	}
	
	function sendEmail()
	{
		var msg = "";
		
		if ($("#subject").val() == "")
		{
			msg = "Please enter your subject";
			$("#subject").focus();
		}
		
		if (editor.getData() == "")
		{
			msg += msg != "" ? " and body email" : "Please enter your body email";
		}
		
		var to = document.getElementById("to");		
		var txtNumDaysAbsent = document.getElementById("txtNumDaysAbsent");
		if (to.selectedIndex == 3 && ($("#txtNumDaysAbsent").val() == "" || isNaN($("#txtNumDaysAbsent").val())))
		{
			msg += msg != "" ? "\nPlease enter the absent days" : "Please enter the absent days";
		}
		
		if (msg != "")
		{
			alert(msg);
		}
		else
		{
			$("#loadingDataAjax").fadeIn();
			$("#content").val(editor.getData());
			var data = $("#tblSendEmail *").serialize();			
			$.post(
				'send_email_functions.php',
				data,
				function(args)
				{
					if (args == "-1")
					{
						alert("Not found any students to send email");
					}
					else
					{
						alert( args + "\nEmail was sent to the students");
					}
					$("#loadingDataAjax").fadeOut();					
				}
			);
		}
	}
	
	function clearContentEmail()
	{
		if (confirm("Do you want to clear all text in email?"))
		{
			$("#titleEmail").val("");
			editor.setData("");
		}
	}
</script>